package cn.akwangl.design.pattern.creational.singleton;

/**
 * 单例模式v3.静态单例,基于类初始化的延迟加载解决方案
 *
 * <p>
 * 第一次加载{@link StaticInnerClassSingleton}类时不会去初始化{@link InnerClass#staticInnerClassSingleton},
 * 只有第一次调用getInstance,虚拟机加载{@link InnerClass}并初始化{@link InnerClass#staticInnerClassSingleton},
 * 这样不仅能确保线程安全,也能保证 {@link StaticInnerClassSingleton}类的唯一性。
 * </p>
 *
 * @author youyongkun
 * @date 2021/11/24 16:32
 */
public class StaticInnerClassSingleton {

    private StaticInnerClassSingleton() {
        if (InnerClass.staticInnerClassSingleton != null) {
            throw new RuntimeException("bean singleton creation is not allowed");
        }
    }

    private static class InnerClass {
        private static StaticInnerClassSingleton staticInnerClassSingleton = new StaticInnerClassSingleton();
    }

    public static StaticInnerClassSingleton instance() {
        return InnerClass.staticInnerClassSingleton;
    }


}
